
#include "Board.h"
#include <iostream>
using namespace std;

Board::Board(string filename) {
   if (filename == "") {
      Initialize();
   }
   else {
      LoadBoard(filename);
   }
}

std::ostream & Board::SaveBoard (std::ostream & os) {

   os << "<board>" << endl;

   for (int i=0; i<BOARD_LENGTH; i++) {
      for (int j=0; j<BOARD_LENGTH; j++) {
          if (pieceTracker[i][j].GetPiece() != NULL) {
             pieceTracker[i][j].GetPiece()->Save(os, i, j);
          }
      }
   }

   os << "</board>" << endl;

   return os;
}

Piece * Board::GetPiece(BoardPosition pos) {
   return pieceTracker[pos.GetRow()][pos.GetCol()].GetPiece();
}

void Board::Initialize(){

   bool white = true;

   for (int i=0; i<BOARD_LENGTH; i++) {
      pieceTracker[6][i].AddPiece("Pawn", white); // White pawn row
      pieceTracker[1][i].AddPiece("Pawn", !white); // Black pawn row
   }

   // White pieces
   pieceTracker[7][0].AddPiece("Rook", white);
   pieceTracker[7][7].AddPiece("Rook", white);
   pieceTracker[7][1].AddPiece("Knight", white);
   pieceTracker[7][6].AddPiece("Knight", white);
   pieceTracker[7][2].AddPiece("Bishop", white);
   pieceTracker[7][5].AddPiece("Bishop", white);
   pieceTracker[7][3].AddPiece("Queen", white);
   pieceTracker[7][4].AddPiece("King", white);

   // Black pieces
   pieceTracker[0][0].AddPiece("Rook", !white);
   pieceTracker[0][7].AddPiece("Rook", !white);
   pieceTracker[0][1].AddPiece("Knight", !white);
   pieceTracker[0][6].AddPiece("Knight", !white);
   pieceTracker[0][2].AddPiece("Bishop", !white);
   pieceTracker[0][5].AddPiece("Bishop", !white);
   pieceTracker[0][3].AddPiece("Queen", !white);
   pieceTracker[0][4].AddPiece("King", !white);
}

void Board::LoadBoard(std::string filename) {
   
}

bool Board::AddPiece(BoardPosition pos, Piece * curPiece) {
   return pieceTracker[pos.GetRow()][pos.GetCol()].SetPiece(curPiece);
}

Piece * Board::RemovePiece(BoardPosition pos) {
   return pieceTracker[pos.GetRow()][pos.GetCol()].RemovePiece();
}
